using System;
using System.Drawing;
using System.Windows.Forms;
using ctsu.Shared;

namespace ctsu.Admin.Forms.CoreLab
{
	public class SampleChooser3Dlg : System.Windows.Forms.Form
	{
		private BarScan _barscan;
		//private BarScan.BarScanCompleteHandler _barScanComplete;

		public delegate void SelectSampleButtonClickEventHandler(object sender, SelectSamplesEventArgs e);
		public event SelectSampleButtonClickEventHandler SelectSampleClick;

		private System.Windows.Forms.RadioButton rdoManual;
		private System.Windows.Forms.Button Retrieve;
		private System.Windows.Forms.TextBox Num_ID;
		public System.Windows.Forms.RadioButton rdoBatch;
		private System.Windows.Forms.Label lblNum_ID;
		private System.Windows.Forms.Button btnClose;
		private GroupBox gbxLocation;
		private ComboBox Freezer;
		private NumericUpDown Shelf;
		private Label label17;
		private Label label16;
		private NumericUpDown Rack;
		private NumericUpDown Box;
		private Label label15;
		private Label label14;
		public RadioButton rdoLocation;
		private System.ComponentModel.Container components = null;

		public string RdoBatchLabel
		{
			set { this.rdoBatch.Text = value; }
		}

		public SampleChooser3Dlg()
		{
			InitializeComponent();
			//this._barScanComplete += new BarScan.BarScanCompleteHandler(barScan_Complete);
			this._barscan = new BarScan(this, Num_ID, null);
			this._barscan.BarScanComplete += new ctsu.Shared.BarScan.BarScanCompleteHandler(barScan_Complete);
			this.Num_ID.KeyPress += new KeyPressEventHandler(Forms.Utils.txtNumWholePos_KeyPress);
		}

		protected override void Dispose(bool disposing)
		{
			if (disposing)
			{
				if (components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose(disposing);
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.rdoManual = new System.Windows.Forms.RadioButton();
			this.Retrieve = new System.Windows.Forms.Button();
			this.Num_ID = new System.Windows.Forms.TextBox();
			this.rdoBatch = new System.Windows.Forms.RadioButton();
			this.lblNum_ID = new System.Windows.Forms.Label();
			this.btnClose = new System.Windows.Forms.Button();
			this.gbxLocation = new System.Windows.Forms.GroupBox();
			this.Freezer = new System.Windows.Forms.ComboBox();
			this.Shelf = new System.Windows.Forms.NumericUpDown();
			this.label17 = new System.Windows.Forms.Label();
			this.label16 = new System.Windows.Forms.Label();
			this.Rack = new System.Windows.Forms.NumericUpDown();
			this.Box = new System.Windows.Forms.NumericUpDown();
			this.label15 = new System.Windows.Forms.Label();
			this.label14 = new System.Windows.Forms.Label();
			this.rdoLocation = new System.Windows.Forms.RadioButton();
			this.gbxLocation.SuspendLayout();
			((System.ComponentModel.ISupportInitialize)(this.Shelf)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.Rack)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.Box)).BeginInit();
			this.SuspendLayout();
			// 
			// rdoManual
			// 
			this.rdoManual.Location = new System.Drawing.Point(8, 5);
			this.rdoManual.Name = "rdoManual";
			this.rdoManual.Size = new System.Drawing.Size(152, 27);
			this.rdoManual.TabIndex = 42;
			this.rdoManual.Text = "Manual (or Use Scanner)";
			this.rdoManual.CheckedChanged += new System.EventHandler(this.rdo_CheckedChanged);
			// 
			// Retrieve
			// 
			this.Retrieve.Enabled = false;
			this.Retrieve.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.Retrieve.Location = new System.Drawing.Point(12, 152);
			this.Retrieve.Name = "Retrieve";
			this.Retrieve.Size = new System.Drawing.Size(64, 23);
			this.Retrieve.TabIndex = 45;
			this.Retrieve.Text = "Retrieve";
			this.Retrieve.Click += new System.EventHandler(this.Retrieve_Click);
			// 
			// Num_ID
			// 
			this.Num_ID.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.Num_ID.Location = new System.Drawing.Point(122, 96);
			this.Num_ID.MaxLength = 5;
			this.Num_ID.Name = "Num_ID";
			this.Num_ID.Size = new System.Drawing.Size(40, 20);
			this.Num_ID.TabIndex = 0;
			this.Num_ID.TextChanged += new System.EventHandler(this.Num_ID_TextChanged);
			// 
			// rdoBatch
			// 
			this.rdoBatch.Checked = true;
			this.rdoBatch.Location = new System.Drawing.Point(8, 33);
			this.rdoBatch.Name = "rdoBatch";
			this.rdoBatch.Size = new System.Drawing.Size(182, 21);
			this.rdoBatch.TabIndex = 43;
			this.rdoBatch.TabStop = true;
			this.rdoBatch.Text = "From Test In Batch";
			this.rdoBatch.CheckedChanged += new System.EventHandler(this.rdo_CheckedChanged);
			// 
			// lblNum_ID
			// 
			this.lblNum_ID.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.lblNum_ID.Location = new System.Drawing.Point(9, 96);
			this.lblNum_ID.Name = "lblNum_ID";
			this.lblNum_ID.Size = new System.Drawing.Size(112, 16);
			this.lblNum_ID.TabIndex = 46;
			this.lblNum_ID.Text = "Enter batch number:";
			// 
			// btnClose
			// 
			this.btnClose.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			this.btnClose.Location = new System.Drawing.Point(84, 152);
			this.btnClose.Name = "btnClose";
			this.btnClose.Size = new System.Drawing.Size(64, 23);
			this.btnClose.TabIndex = 47;
			this.btnClose.Text = "Close";
			this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
			// 
			// gbxLocation
			// 
			this.gbxLocation.Controls.Add(this.Freezer);
			this.gbxLocation.Controls.Add(this.Shelf);
			this.gbxLocation.Controls.Add(this.label17);
			this.gbxLocation.Controls.Add(this.label16);
			this.gbxLocation.Controls.Add(this.Rack);
			this.gbxLocation.Controls.Add(this.Box);
			this.gbxLocation.Controls.Add(this.label15);
			this.gbxLocation.Controls.Add(this.label14);
			this.gbxLocation.Location = new System.Drawing.Point(12, 96);
			this.gbxLocation.Name = "gbxLocation";
			this.gbxLocation.Size = new System.Drawing.Size(560, 46);
			this.gbxLocation.TabIndex = 245;
			this.gbxLocation.TabStop = false;
			this.gbxLocation.Text = "Enter Box Location";
			this.gbxLocation.Visible = false;
			// 
			// Freezer
			// 
			this.Freezer.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.Freezer.Location = new System.Drawing.Point(72, 16);
			this.Freezer.Name = "Freezer";
			this.Freezer.Size = new System.Drawing.Size(152, 21);
			this.Freezer.TabIndex = 241;
			// 
			// Shelf
			// 
			this.Shelf.Location = new System.Drawing.Point(280, 18);
			this.Shelf.Name = "Shelf";
			this.Shelf.Size = new System.Drawing.Size(48, 20);
			this.Shelf.TabIndex = 2;
			// 
			// label17
			// 
			this.label17.Location = new System.Drawing.Point(10, 18);
			this.label17.Name = "label17";
			this.label17.Size = new System.Drawing.Size(57, 19);
			this.label17.TabIndex = 236;
			this.label17.Text = "Freezer:";
			// 
			// label16
			// 
			this.label16.Location = new System.Drawing.Point(240, 18);
			this.label16.Name = "label16";
			this.label16.Size = new System.Drawing.Size(42, 19);
			this.label16.TabIndex = 237;
			this.label16.Text = "Shelf:";
			// 
			// Rack
			// 
			this.Rack.Location = new System.Drawing.Point(392, 18);
			this.Rack.Name = "Rack";
			this.Rack.Size = new System.Drawing.Size(48, 20);
			this.Rack.TabIndex = 3;
			// 
			// Box
			// 
			this.Box.Location = new System.Drawing.Point(496, 18);
			this.Box.Name = "Box";
			this.Box.Size = new System.Drawing.Size(48, 20);
			this.Box.TabIndex = 4;
			// 
			// label15
			// 
			this.label15.Location = new System.Drawing.Point(344, 18);
			this.label15.Name = "label15";
			this.label15.Size = new System.Drawing.Size(42, 19);
			this.label15.TabIndex = 238;
			this.label15.Text = "Rack:";
			// 
			// label14
			// 
			this.label14.Location = new System.Drawing.Point(456, 18);
			this.label14.Name = "label14";
			this.label14.Size = new System.Drawing.Size(35, 19);
			this.label14.TabIndex = 239;
			this.label14.Text = "Box:";
			// 
			// rdoLocation
			// 
			this.rdoLocation.Location = new System.Drawing.Point(8, 60);
			this.rdoLocation.Name = "rdoLocation";
			this.rdoLocation.Size = new System.Drawing.Size(150, 21);
			this.rdoLocation.TabIndex = 246;
			this.rdoLocation.Text = "From Box Location";
			// 
			// SampleChooser3Dlg
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(584, 186);
			this.Controls.Add(this.rdoLocation);
			this.Controls.Add(this.gbxLocation);
			this.Controls.Add(this.btnClose);
			this.Controls.Add(this.rdoManual);
			this.Controls.Add(this.Retrieve);
			this.Controls.Add(this.Num_ID);
			this.Controls.Add(this.rdoBatch);
			this.Controls.Add(this.lblNum_ID);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
			this.Name = "SampleChooser3Dlg";
			this.Text = "Select Draw Labels";
			this.Load += new System.EventHandler(this.SampleChooser3Dlg_Load);
			this.gbxLocation.ResumeLayout(false);
			((System.ComponentModel.ISupportInitialize)(this.Shelf)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.Rack)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.Box)).EndInit();
			this.ResumeLayout(false);
			this.PerformLayout();

		}
		#endregion

		private void barScan_Complete(object sender, BarScanEventArgs e)
		{
			if (e.ID > -1)
				Retrieve_Click(sender, new System.EventArgs());
		}

		private void Num_ID_TextChanged(object sender, System.EventArgs e)
		{
			if (Num_ID.Text.Length > 0)
			{
				if ((Convert.ToInt32(Num_ID.Text)) > 0)
					Retrieve.Enabled = true;
				else
					Retrieve.Enabled = false;
			}
			else
				Retrieve.Enabled = false;
		}

		private void Retrieve_Click(object sender, System.EventArgs e)
		{

			SelectSamplesEventArgs ev = new SelectSamplesEventArgs();
			if (Num_ID.Visible)
				ev.ID = Convert.ToInt32(Num_ID.Text);
			else
				ev.Location = GetFormatatedLocation();

			if (rdoBatch.Checked)
				ev.SelectType = 0;
			else if (rdoManual.Checked)
				ev.SelectType = 1;
			else
				ev.SelectType = 2;

			if (SelectSampleClick != null)
				this.SelectSampleClick(this, ev);
			if (Num_ID.Visible)
				this.Num_ID.Focus();
			else
				gbxLocation.Focus();
		}

		private string GetFormatatedLocation()
		{
			string location = "Fz:" + Freezer.SelectedValue.ToString() + "," +
				"Sf:" + Shelf.Value.ToString() + "," +
				"Rk:" + Rack.Value.ToString() + "," +
				"Bx:" + Box.Value.ToString();
			return location;
		}

		private void rdo_CheckedChanged(object sender, System.EventArgs e)
		{
			if (this.rdoBatch.Checked)
			{
				gbxLocation.Visible = false;
				lblNum_ID.Visible = true;
				Num_ID.Visible = true;
				lblNum_ID.Text = "Enter Batch Number:";
				this.Num_ID.Focus();
			}
			else if (rdoManual.Checked)
			{
				gbxLocation.Visible = false;
				lblNum_ID.Visible = true;
				Num_ID.Visible = true;
				lblNum_ID.Text = "Enter Label ID:";
				this.Num_ID.Focus();
			}
			else
			{
				Retrieve.Enabled = true;
				lblNum_ID.Visible = false;
				Num_ID.Visible = false;
				gbxLocation.Location = new Point(8, 96);
				gbxLocation.Visible = true;
				gbxLocation.Focus();
			}

			if (Num_ID.Visible)
			{
				if (Num_ID.Text.Length == 0)
					Retrieve.Enabled = false;
			}

		}

		private void btnClose_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void SampleChooser3Dlg_Load(object sender, EventArgs e)
		{
			Forms.Utils.LoadCbo(Freezer, "Freezer_L", true);
		}
	}


}
